// pages/shopcar/shopcar.js
const app = getApp();

Page({
  data: {
    uploadUrl   : '',
    isAllSelect : false,
    price       : 0,
    haschose    : false,
    arr         : [],
    carts       : [
      // {
      //   cartsid : 0,
      //   pic     : "",
      //   name    : "",
      //   norms   : "",
      //   stock   : 0,
      //   price   : "",
      //   status  : false,
      //   num     : 0,
      //   // 数据设定
      //   count   : {
      //     // quantity  : 1,
      //     // min       : 1,
      //     // max       : 20
      //   },
      // },
    ],
  },
  //此处2为保留两位小数
  toFix: function (value) { return value.toFixed(2)},
  //总价
  price: function () {
    console.log('执行函数')
    let that   = this;
    var carts  = that.data.carts;
    var p      = 0;
    var cartid = []
    for (var i = 0; i < carts.length; i++) {
      var s    = carts[i].price * carts[i].num
      if (carts[i].status == true) {
        p = p + s; cartid.push(carts[i].cartid)
      } else {
       
      }
      that.setData({
        price: p, carts: carts, cartid: cartid
      })
    }
  },
  // 增加数量
  addCount(e) {
    let that    = this;
    var cartsid = e.currentTarget.id;
    var index   = e.currentTarget.dataset.index;
    var carts   = that.data.carts;
    carts[index].num = carts[index].num + 1;
    wx.request({
      url     : app.url +'/Goods/cart_num',
      method  : 'post',
      data    : { cartsid : cartsid,num : carts[index].num },
      success : function(res){
        that.setData({ carts : carts }); that.price()
      },
      error   : function(){}
    });
  },
  // 减少数量
  minusCount(e) {
    let that    = this;
    var cartsid = e.currentTarget.id;
    var index   = e.currentTarget.dataset.index;
    var carts   = that.data.carts;
    if (carts[index].num <= 1) {
      return false
    } else {
      carts[index].num = carts[index].num - 1
    }
    wx.request({
      url     : app.url + '/Goods/cart_num',
      method  : 'post',
      data    : { cartsid: cartsid, num: carts[index].num },
      success : function (res) {
        that.setData({  carts: carts  });
        that.price()
      },
      error: function () { }
    });
    that.price()
  },
  //选择
  switchSelect: function (e) {
    let that    = this;
    var cartsid = e.currentTarget.id;
    var index   = e.currentTarget.dataset.index;
    var carts   = that.data.carts;
    var arr     = that.data.arr;
    carts[index].status = !carts[index].status
    wx.request({
      url     : app.url + '/Goods/cart_select',
      method  : 'post',
      data: { cartsid: cartsid, status: carts[index].status},
      success : function(res){
        arr = arr.concat(carts[index]);
        that.setData({ carts: carts, arr: arr });
        that.price();
      },
      error   : function(){} 
    })  
  },
  //全选
  allSelect: function (e) {
    let that        = this;
    var carts       = that.data.carts; 
    var isAllSelect = !that.data.isAllSelect;
    if (isAllSelect) {
      for(var i = 0; i < carts.length; i++){ carts[i].status = true }
      that.setData({ carts: carts })
    }else{
      for(var i = 0; i < carts.length; i++){ carts[i].status = false }
      that.setData({ carts: carts })
    }
    that.setData({ isAllSelect: isAllSelect }); that.price();
  },
  //删除
  delete: function (e) {
    let that   = this;
    var carts  = that.data.carts;
    var arr    = that.data.arr;
    var cartid = ''; 
    var uid    = wx.getStorageSync('uid');
    for (var i = 0; i < carts.length;i++){
      if (carts[i].status == 1){
        cartid += carts[i].cartsid+','
      }
    }
    if (cartid == false || cartid == null || cartid == ''){
      wx.showToast({  title: '未选择商品' });  return false ;
    }
    wx.request({
      url     : app.url + '/Goods/cart_del',
      method  : 'post',
      data    : {cartid : cartid,uid : uid}, 
      success : function(res){
        wx.navigateTo({ url: '../index/index' })
      },
      error   :function(){}
    })
  },
  jiesuan: function (e) {
    let that    = this;
    var uid     = wx.getStorageSync('uid');
    var cartids ='';
    // for (var i = 0; i < that.data.carts.length; i++) {
    //   if (that.data.carts[i].status == true){
    //     cartids += that.data.carts[i]['cartsid']+','
    //   }else{
    //     wx.showToast({ title: '请选择商品' }); return false;
    //   }
    // }
    var carts = that.data.carts;
    for (var i = 0; i < carts.length; i++) {
      if (carts[i].status == 1) {
        cartids += carts[i].cartsid + ','
      }
    }
    wx.navigateTo({ url: '../byorder/byorder?cartids=' + cartids +'&type=0' })
  },
  onShow: function () {
    var that = this;
    var uid  = wx.getStorageSync('uid');
    wx.request({
      url     : app.url+'/Goods/cart_list',
      method  : 'post',
      data    : {uid :uid },
      success : function(res){
        if(res.data.code == 1){
          that.setData({ carts: res.data.data }) 
        }else{
          wx.showToast({ title: '暂无数据' })
        }
      },
      error : function(){}
    })
  },
  onLoad: function (options) {
    
  },
  onReady: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  },
  mall: function () {
    wx.redirectTo({ url: "../mall/mall" })
  },
  issue: function () {
    wx.navigateTo({ url: "../issue/issue"})
  },
  index: function () {
    wx.redirectTo({ url: "../index/index"})
  },
  mine: function () {
    wx.redirectTo({ url: "../mine/mine" })
  },
})